package com.itheima.controller;

import com.itheima.anno.Mylog;
import com.itheima.pojo.GoodPurchase;
import com.itheima.pojo.GoodsPurchaseQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.GoodsPurchaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/supermarket/purchase")
public class GoodsPurchaseController {

    @Autowired
    private GoodsPurchaseService goodsPurchaseService;

    /**
     * 新增采购
     * @param goodPurchase
     * @return
     */
    @Mylog
    @PostMapping("/create")
    public Result add(@RequestBody GoodPurchase goodPurchase){
        log.info("新增采购记录 {}",goodPurchase);
        goodsPurchaseService.add(goodPurchase);
        return Result.success();
    }
    @Mylog
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除采购记录");
        goodsPurchaseService.delete(id);
        return Result.success();
    }

    /**
     * 修改采购记录
     * @param goodPurchase
     * @return
     */
    @Mylog
    @PutMapping("/update")
    public Result update(@RequestBody GoodPurchase goodPurchase){
        log.info("修改采购记录 {}",goodPurchase);
        goodsPurchaseService.update(goodPurchase);
        return Result.success();
    }

    @GetMapping("/query")
    public Result page(GoodsPurchaseQueryParam goodsPurchaseQueryParam){
        log.info("data:"+goodsPurchaseQueryParam);
        PageBean pageBean = goodsPurchaseService.page(goodsPurchaseQueryParam);
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Mylog
    @DeleteMapping("/batch/delete")
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除采购记录 {}",ids);
        goodsPurchaseService.deleteByIds(ids);
        return Result.success();
    }

}
